import { defineStore } from 'pinia'
import { type Scene, Group, PerspectiveCamera, WebGLRenderer } from 'three'
import {
  CSS2DRenderer,
  EffectComposer,
  TransformControls,
  ViewHelper
} from 'three/examples/jsm/Addons'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import { computed, shallowRef } from 'vue'

export const useViewportStore = defineStore('viewport', () => {
  // 画布
  const canvas = shallowRef<HTMLCanvasElement>()
  function setCanvas(dom: HTMLCanvasElement) {
    canvas.value = dom
  }
  // 场景
  const scene = shallowRef<Scene>()
  function setScene(scen: Scene) {
    scene.value = scen
  }
  // 不公开组
  const notPublicGroup = shallowRef<Group>()
  function setNotPublicGroup(group: Group) {
    notPublicGroup.value = group
  }
  // 公开组
  const publicGroup = shallowRef<Group>()
  function setPublicGroup(group: Group) {
    publicGroup.value = group
  }
  // 相机
  const camera = shallowRef<PerspectiveCamera>()
  function setCamera(camer: PerspectiveCamera) {
    camera.value = camer
  }
  // 2D渲染器
  const css2DRenderer = shallowRef<CSS2DRenderer>()
  function setCss2DRenderer(renderer: CSS2DRenderer) {
    css2DRenderer.value = renderer
  }
  // WebGL渲染器
  const renderer = shallowRef<WebGLRenderer>()
  function setRenderer(render: WebGLRenderer) {
    renderer.value = render
  }
  // 轨道控制器
  const orbitControls = shallowRef<OrbitControls>()
  function setOrbitControls(controls: OrbitControls) {
    orbitControls.value = controls
  }
  // 变换控制器
  const transformControls = shallowRef<TransformControls>()
  function setTransformControls(controls: TransformControls) {
    transformControls.value = controls
  }
  // 后处理
  const composer = shallowRef<EffectComposer>()
  function setComposer(compose: EffectComposer) {
    composer.value = compose
  }
  // 视图助手
  const viewHelper = shallowRef<ViewHelper>()
  function setViewHelper(helper: ViewHelper) {
    viewHelper.value = helper
  }
  return {
    canvas,
    setCanvas,
    scene,
    setScene,
    notPublicGroup,
    setNotPublicGroup,
    publicGroup,
    setPublicGroup,
    camera,
    setCamera,
    css2DRenderer,
    setCss2DRenderer,
    renderer,
    setRenderer,
    orbitControls,
    setOrbitControls,
    transformControls,
    setTransformControls,
    composer,
    setComposer,
    viewHelper,
    setViewHelper
  }
})
